package com.weimob.mcloud.wechatwork.customer.service.impl.convert;

import com.weimob.mcloud.wechatwork.core.entity.base.Person;
import com.weimob.mcloud.wechatwork.core.entity.relation.employee.Employee;
import com.weimob.mcloud.wechatwork.core.entity.relation.group.CustomerGroup;
import com.weimob.mcloud.wechatwork.core.entity.relation.group.CustomerGroupMember;
import com.weimob.mcloud.wechatwork.customer.service.user.module.bo.group.CustomerChatGroupBO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;

import java.util.Map;
import java.util.Objects;

/**
 * @author chenliuzhu
 * @date 2023/3/10
 */
@Slf4j
public class CustomerGroupConvert {


    /**
     *  模型转换
     * @param memberId
     * @param customerGroupMemberMap
     * @param customerGroup
     * @return
     */
    public static CustomerChatGroupBO getCustomerChatGroupBO(String memberId, Map<String, CustomerGroupMember> customerGroupMemberMap, CustomerGroup customerGroup) {
        if(Objects.isNull(customerGroup)){
            log.error("CustomerGroupConvert.getCustomerChatGroupBO 入参 customerGroup 为null" );
            return new CustomerChatGroupBO();
        }
        CustomerChatGroupBO customerChatGroupBO = new CustomerChatGroupBO();
        customerChatGroupBO.setChatId(customerGroup.getGroupId());
        customerChatGroupBO.setName(customerGroup.getName());
        Person owner = customerGroup.getOwner();
        if (Objects.nonNull(owner)) {
            Employee employee = (Employee) owner;
            customerChatGroupBO.setOwner(employee.getId());
            customerChatGroupBO.setOwnerName(employee.getName());
            customerChatGroupBO.setHaveBindGuide(!CollectionUtils.isEmpty(employee.getGuiderList()));
        }

        customerChatGroupBO.setNum(customerGroup.getNum());
        // 入群时间
        if (MapUtils.isNotEmpty(customerGroupMemberMap)) {
            CustomerGroupMember member = customerGroupMemberMap.get(customerGroup.getGroupId());
            if (Objects.nonNull(member) && Objects.nonNull(member.getJoinTime())) {
                customerChatGroupBO.setJoinTime(member.getJoinTime().getTime());
            }
        }
        customerChatGroupBO.setGroupCreateTime(customerGroup.getCreateTime().getTime());
        return customerChatGroupBO;
    }
}
